package net.recursv.motific.atweaver

/**
 * Created by: recursv
 * Date: 14/08/2009
 * Time: 11:34:19 AM
 */

public class MidletTransformerTest extends GroovyTestCase {
  void testMidletDetection() {
    assertTrue(MidletTransformer.isMidlet("public class SimpleMidlet extends MIDlet {}"))
    assertFalse(MidletTransformer.isMidlet("public class SimpleMidlet extends RandomClass {}"))
    assertFalse(MidletTransformer.isMidlet("public class SimpleMidlet {}"))

  }

  void testImportInsertion() {
    String mod = MidletTransformer.addImports("""package net.recursv.motific.samples.at;
                                            import javax.microedition.lcdui.*;
                                            import javax.microedition.midlet.MIDlet;
                                            import javax.microedition.midlet.MIDletStateChangeException;
                                            public class SimpleMidlet extends MIDlet {}""")

    assertTrue(mod.contains("package net.recursv.motific.samples.at;"))
    assertTrue(mod.contains("import net.recursv.motific.at.impl.__Canvas;"))
    assertTrue(mod.contains("import net.recursv.motific.at.impl.__MIDlet;"))
    assertTrue(mod.contains("import javax.microedition.lcdui.*;"))
    assertTrue(mod.contains("import javax.microedition.midlet.MIDletStateChangeException;"))

    assertFalse(mod.contains("import javax.microedition.midlet.MIDlet;"))
  }

  void testImplementsInterface() {
    String mod = MidletTransformer.implementInterface("""public class SimpleMidlet extends MIDlet {
                                                          protected void startApp() throws MIDletStateChangeException { startApp(); }
                                                          protected void pauseApp() { pauseApp(); }
                                                          protected void destroyApp(boolean b) throws MIDletStateChangeException { destroyApp(b); }
                                                         }""")

    assertTrue(mod.contains("public class SimpleMidlet implements __MIDlet"))

    assertTrue(mod.contains("public void startApp() throws MIDletStateChangeException { startApp(); __startAppCalled = true;}"))
    assertTrue(mod.contains("public void pauseApp() { pauseApp(); __pauseAppCalled = true;}"))
    assertTrue(mod.contains("public void destroyApp(boolean b) throws MIDletStateChangeException { destroyApp(b); __destroyAppCalled = true;}"))

    assertTrue(mod.contains("public void notifyDestroyed() {try{destroyApp(true);} catch (MIDletStateChangeException ignored) {}}"))
    assertTrue(mod.contains("public void paint(GraphicsWrapper g) {if (__display != null) __display.doPaint(g);}"))
    assertTrue(mod.contains("public void keyPress(int keyCode) {__display.doKeyPress(keyCode);}"))
    assertTrue(mod.contains("public boolean isStartAppCalled() {return __startAppCalled;}"))
    assertTrue(mod.contains("public boolean isDestroyAppCalled() {return __destroyAppCalled;}"))
    assertTrue(mod.contains("public boolean isPauseAppCalled() {return __pauseAppCalled;}"))
  }

  void testImplementInterfaceComplex() {
    String mod = MidletTransformer.implementInterface("public class SimpleMidlet extends MIDlet implements Listener {}")

    assertTrue(mod.contains("public class SimpleMidlet implements Listener,__MIDlet"))
  }

  void testFieldInsertion() {
    String mod = MidletTransformer.insertFields("public class SimpleMidlet implements __MIDlet {}")

    assertTrue(mod.contains("private __Canvas __display;"))
    assertTrue(mod.contains("private boolean __startAppCalled;"))
    assertTrue(mod.contains("private boolean __destroyAppCalled;"))
    assertTrue(mod.contains("private boolean __pauseAppCalled;"))
  }

}
